﻿#region Copyright
//       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
// 
//        This program is free software: you can redistribute it and/or modify
//        it under the terms of the GNU General Public License as published by
//        the Free Software Foundation, either version 3 of the License, or
//        (at your option) any later version.
// 
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
// 
//        You should have received a copy of the GNU General Public License
//        along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;

namespace FT.Architecture.Utils.ExtensionMethods
{
    /// <summary>
    /// Extensions for Type
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// Determines if <paramref name="type"/> is <paramref name="typeToCompareTo"/>.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="typeToCompareTo"></param>
        /// <returns></returns>
        public static bool Is(this Type type, Type typeToCompareTo)
        {
            if (type == typeToCompareTo)
                return true;

            if (type.IsSubclassOf(typeToCompareTo))
                return true;

            if (typeToCompareTo.IsAssignableFrom(type))
                return true;

            return false;
        }

        /// <summary>
        /// Determines if <paramref name="type"/> is <typeparam name="T" />.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool Is<T>(this Type type)
        {
            Type tType = typeof (T);
            return Is(type, tType);
        }
    }
}